package com.weiyi.mashangzhu.Weight.dialog;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.FrameLayout;

import com.uclbrt.sdk.fetcher.KeyGenerator;
import com.uclbrt.sdk.fetcher.KeyInvalid;
import com.uclbrt.sdk.fetcher.listener.KeyGeneratorListener;
import com.uclbrt.sdk.fetcher.listener.KeyInvalidListener;
import com.uclbrt.sdk.view.fragment.KeyFetcherFragment;
import com.uclbrt.sdk.view.listener.KeyFetcherViewListener;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.listener.LoadingView;
import com.weiyi.mashangzhu.uclbrt.Config;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BoxDialog extends FragmentActivity implements LoadingView {

    @BindView(R.id.content)
    FrameLayout content;

    private KeyGenerator mKeyGenerator;
    private KeyInvalid mKeyInvalid;
    private KeyFetcherFragment mKeyFetcherFragment;
    private ProgressDialog mProgressDialog;
    private Handler mHandler;
    private SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmm");
    // 2c912b766057dd3e016057fc043c0003@38@15098406972    18826401470
    // 伪造数据
    String mobile = ""; // 手机号
    String roomNo = ""; // 锁掌柜房间号
    String buildingNo = "";
    String floorNo = "";
    String startTimeStr = "";
    String endTimeStr = "";
    String starttime, endtime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_box);
        ButterKnife.bind(BoxDialog.this);
        MyApp.getInstance().addActivity(this);
        Intent intent = getIntent();
//        roomNo = intent.getStringExtra("RoomNo");
//        buildingNo = intent.getStringExtra("BuildNo");
//        floorNo = intent.getStringExtra("FloorNo");

        roomNo = "001";
        buildingNo = "001";
        floorNo = "001";

        starttime = intent.getStringExtra("startTime");
        endtime = intent.getStringExtra("endTime");
        Log.e("ttttttttttttttttttttt", "s=" + starttime);
        Log.e("ttttttttttttttttttttt", "e=" + endtime);
        if (roomNo != null && buildingNo != null && floorNo != null && !roomNo.equals("") && !buildingNo.equals("") && !floorNo.equals("")) {
            String token = SPUtils.get(this, "token", "") + "";
            if (!token.equals("")) {
                mobile = token.split("@")[2];
            } else {
                mobile = "11111111111";
            }
            if (starttime != null && endtime != null && !starttime.equals("") && !endtime.equals("")) {
                String stime[] = starttime.split("-");
                String etime[] = endtime.split("-");
                startTimeStr = stime[0].substring(2) + stime[1] + stime[2] + "1400";
                endTimeStr = etime[0].substring(2) + etime[1] + etime[2] + "1200";
                Log.e("ttttttttttttttttttttt", "es=" + startTimeStr);
                Log.e("ttttttttttttttttttttt", "es=" + endTimeStr);
                com.weiyi.mashangzhu.Utils.ProgressDialog.showProgressDialog(this);
                Log.e("vvvvvvvvvvvvvvvvvv", "=" + startTimeStr);
                Log.e("vvvvvvvvvvvvvvvvvv", "=" + endTimeStr);
                Log.e("vvvvvvvvvvvvvvvvvv", "=" + mobile);
                Log.e("vvvvvvvvvvvvvvvvvv", "=" + roomNo);
                Log.e("vvvvvvvvvvvvvvvvvv", "=" + buildingNo);
                Log.e("vvvvvvvvvvvvvvvvvv", "=" + floorNo);
                initData(startTimeStr, endTimeStr, mobile, roomNo, buildingNo, floorNo);
                initTime(startTimeStr, endTimeStr, mobile, roomNo, buildingNo, floorNo);
                setResult();
            } else {
                T.showShort(BoxDialog.this, "入住时间获取失败");
            }

        } else {
            T.showShort(BoxDialog.this, "房源信息获取失败");
        }

    }

    private void initData(final String start, final String end, final String phone, final String room, final String building, final String floor) {
        mHandler = new Handler(Looper.getMainLooper());
        mProgressDialog = new ProgressDialog(BoxDialog.this);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage("正在生成房卡，请稍后...");
        mProgressDialog.setIndeterminate(false);
        // 生成房卡类
        mKeyGenerator = new KeyGenerator(Config.UCLBRT_SID, Config.UCLBRT_TOKEN);
        mKeyGenerator.setCommunityNo(Config.UCLBRT_COMMUNITY_NO);
        mKeyGenerator.setListener(new KeyGeneratorListener() {
            @Override
            public void onSuccess(final String no) {
                // TODO:后台线程处理返回结果
                // TODO: .使用Handler通知主线程更新UI
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        hideLoading();
                        showLoading();
                        Log.e("vvvvvvvvvvvvvvvvvv", "onSuccess=" + phone);
                        Log.e("vvvvvvvvvvvvvvvvvv", "onSuccess=" + room);
                        Log.e("vvvvvvvvvvvvvvvvvv", "onSuccess=" + building);
                        Log.e("vvvvvvvvvvvvvvvvvv", "onSuccess=" + floor);
                        Log.e("vvvvvvvvvvvvvvvvvv", "onSuccess=" + start);
                        Log.e("vvvvvvvvvvvvvvvvvv", "onSuccess=" + end);

                        mKeyFetcherFragment.setMobile(phone);
                        mKeyFetcherFragment.setRoomNo(room);
                        mKeyFetcherFragment.setBuildNo(building);
                        mKeyFetcherFragment.setFloorNo(floor);
                        mKeyFetcherFragment.setStartTime(start);
                        mKeyFetcherFragment.setEndTime(end);
                        mKeyFetcherFragment.requestKey();
                        com.weiyi.mashangzhu.Utils.ProgressDialog.setDialogResult();
                    }
                });
            }

            @Override
            public void onError(final String message, final int status) {
                // status已经定义在锁掌柜的状态码文档
                // TODO:后台线程处理返回结果，status已经定义在锁掌柜的状态码文档
                // TODO: .使用Handler通知主线程更新UI
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        hideLoading();
                        showError("onError=" + status + " " + message);
                        com.weiyi.mashangzhu.Utils.ProgressDialog.dismissProgressDialog();
                    }
                });
            }
        });
        // 失效房卡类
        mKeyInvalid = new KeyInvalid(Config.UCLBRT_SID, Config.UCLBRT_TOKEN);
        mKeyInvalid.setCommunityNo(Config.UCLBRT_COMMUNITY_NO);
        mKeyInvalid.setListener(new KeyInvalidListener() {
            @Override
            public void onSuccess() {
                // TODO:后台线程处理返回结果
                // TODO: .使用Handler通知主线程更新UI
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        hideLoading();
                        T.showShort(BoxDialog.this, "失效记录成功");
                        com.weiyi.mashangzhu.Utils.ProgressDialog.dismissProgressDialog();
                    }
                });
            }

            @Override
            public void onError(final String message, int status) {
                // status已经定义在锁掌柜的状态码文档
                // TODO:后台线程处理返回结果，status已经定义在锁掌柜的状态码文档
                // TODO: .使用Handler通知主线程更新UI
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        hideLoading();
                        showError(message);
                        com.weiyi.mashangzhu.Utils.ProgressDialog.dismissProgressDialog();
                    }
                });
            }
        });


        mKeyFetcherFragment = new KeyFetcherFragment();
        mKeyFetcherFragment.setId(Config.UCLBRT_SID);
        mKeyFetcherFragment.setToken(Config.UCLBRT_TOKEN);
        mKeyFetcherFragment.setCommunityNo(Config.UCLBRT_COMMUNITY_NO);
        mKeyFetcherFragment.setListener(new KeyFetcherViewListener() {
            @Override
            public void onError(String message, int status) {
                // status已经定义在锁掌柜的状态码文档
                // TODO：请求结果错误回调
                hideLoading();
                T.showShort(BoxDialog.this, message);
                com.weiyi.mashangzhu.Utils.ProgressDialog.dismissProgressDialog();
            }

            @Override
            public void onSuccess() {
                // TODO：请求结果成功回调
                hideLoading();
                T.showShort(BoxDialog.this, "获取成功");
                com.weiyi.mashangzhu.Utils.ProgressDialog.dismissProgressDialog();
            }
        });
    }

    private void initTime(final String start, final String end, String phone, String room, String building, String floor) {
        mKeyGenerator.create(phone, building, floor, room, start, end);
    }

    private void setResult() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.content, mKeyFetcherFragment);
        transaction.commit();
    }


    @OnClick(R.id.card_close)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showLoading() {
//        mProgressDialog.show();
    }

    @Override
    public void hideLoading() {
        mProgressDialog.hide();
    }

    @Override
    public void showError(String message) {
        T.showShort(BoxDialog.this, message);
    }
}
